package com.axend.vetwavve.viewmodel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.axend.lib_base.base.vm.BaseViewModel
import com.axend.lib_base.ext.roundToFloatOneDecimal
import com.axend.lib_base.ext.toStrOrDefault
import com.axend.lib_room.entity.PetInfoEntity
import com.axend.lib_room.manager.PetInfoManager
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class StartMonitoringViewModel : BaseViewModel() {
    private var _petInfoEntity: MutableLiveData<PetInfoEntity> = MutableLiveData()
    val petInfoEntity: MutableLiveData<PetInfoEntity> get() = _petInfoEntity

    /*体表最高温*/
    private var _bodyMaxTemp: MutableLiveData<String> = MutableLiveData("--")
    val bodyMaxTemp: MutableLiveData<String> get() = _bodyMaxTemp

    /*体表最高温ecg*/
    private var _bodyMaxTempEcg: MutableLiveData<String> = MutableLiveData("--")
    val bodyMaxTempEcg: MutableLiveData<String> get() = _bodyMaxTempEcg

    /*呼吸率*/
    private var _breathRate: MutableLiveData<String> = MutableLiveData("--")
    val breathRate: MutableLiveData<String> get() = _breathRate

    /*呼吸率ecg*/
    private var _breathRateEcg: MutableLiveData<String> = MutableLiveData("--")
    val breathRateEcg: MutableLiveData<String> get() = _breathRateEcg

    /*心率*/
    private var _heartRate: MutableLiveData<String> = MutableLiveData("--")
    val heartRate: MutableLiveData<String> get() = _heartRate

    /*心率ecg*/
    private var _heartRateEcg: MutableLiveData<String> = MutableLiveData("--")
    val heartRateEcg: MutableLiveData<String> get() = _heartRateEcg

    /*距离*/
    private var _distance: MutableLiveData<String> = MutableLiveData("0")
    val distance: MutableLiveData<String> get() = _distance

    /*是否暂停*/
    private var _isPause: MutableLiveData<Boolean> = MutableLiveData(false)
    val isPause: MutableLiveData<Boolean> get() = _isPause

    /*体动*/
    private var _isMove: MutableLiveData<Boolean> = MutableLiveData(false)
    val isMove: MutableLiveData<Boolean> get() = _isMove

    /*是否展示曲线图*/
    private var _isShowCurve: MutableLiveData<Boolean> = MutableLiveData(false)
    val isShowCurve: MutableLiveData<Boolean> get() = _isShowCurve

    /*是否报警*/
    private var _isAlarm: MutableLiveData<Boolean> = MutableLiveData(false)
    val isAlarm: MutableLiveData<Boolean> get() = _isAlarm

    /*是否断开连接*/
    private var _isDisconnect: MutableLiveData<Boolean> = MutableLiveData(false)
    val isDisconnect: MutableLiveData<Boolean> get() = _isDisconnect

    /*是否录制红外屏幕*/
    private var _isRecord: MutableLiveData<Boolean> = MutableLiveData(false)
    val isRecord: MutableLiveData<Boolean> get() = _isRecord



    fun setPetInfoEntity(petInfoEntity: PetInfoEntity) {
        _petInfoEntity.value = petInfoEntity
    }

    fun setIsPause(isPause: Boolean) {
        _isPause.postValue(isPause)
    }

    /**
     * 设置温度(延迟1秒非阻塞)
     */
    fun setBodyMaxTemp(temp: String) {
        launchOnUI {
            _bodyMaxTemp.postValue(temp)
        }
    }

    /**
     * 设置呼吸率
     */
    fun setBreathRate(rate: String) {
        launchOnUI {
            _breathRate.postValue(rate)
        }
    }

    /**
     * 设置心率
     */
    fun setHeartRate(rate: String) {
        launchOnUI {
            _heartRate.postValue(rate)
        }
    }

    fun setBodyMaxTempEcg(temp: String) {
        launchOnUI {
            _bodyMaxTempEcg.postValue(temp)
        }
    }

    fun setBreathRateEcg(rate: String) {
        launchOnUI {
            _breathRateEcg.postValue(rate)
        }
    }

    fun setHeartRateEcg(rate: String) {
        launchOnUI {
            _heartRateEcg.postValue(rate)
        }
    }


    /**
     * 设置距离
     */
    fun setDistance(distance: String) {
        launchOnUI {
            _distance.postValue(distance)
        }
    }

    /**
     * 设置是否体动
     */
    fun setIsMove(isMove: Boolean) {
        launchOnUI {
            _isMove.postValue(isMove)
        }
    }


    /**
     * 设置是否展示曲线图
     */
    fun setIsShowCurve(isShowCurve: Boolean) {
        launchOnUI {
            _isShowCurve.postValue(isShowCurve)
        }
    }


    /**
     * 设置报警
     */
    fun setIsAlarm(isAlarm: Boolean) {
        launchOnUI {
            _isAlarm.postValue(isAlarm)
        }
    }


    /**
     * 根据id查询宠物档案
     */
    fun getPetInfoById(id: Int) {
        viewModelScope.launch {
            val petInfoEntity = PetInfoManager.getPetInfoItem(id)
            petInfoEntity?.let {
                _petInfoEntity.value = it
            }
        }
    }


    /**
     * 更新数据到数据库
     */
    fun updateParamsToDataBase(
        petInfoEntity: PetInfoEntity,
        callback: (petInfoEntity: PetInfoEntity) -> Unit
    ) {
        launchOnUI {
            withContext(Dispatchers.IO) {
                try {
                    PetInfoManager.updatePetInfoItem(petInfoEntity)
                } catch (e: Exception) {
                    e.printStackTrace()
                }
            }
//            delay(2000L)
            callback(petInfoEntity)
        }
    }


    /**
     * 是否断开连接
     */
    fun setIsDisconnect(isDisconnect: Boolean) {
        launchOnUI {
            _isDisconnect.postValue(isDisconnect)
        }
    }


    /**
     * 是否录制红外屏
     */
    fun setIsRecord(isRecord: Boolean) {
        launchOnUI {
            _isRecord.postValue(isRecord)
        }
    }


}